package com.scen.sys.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scen.sys.common.constast;
import com.scen.sys.common.DataGridView;
import com.scen.sys.common.ResultObj;
import com.scen.sys.common.TreeNode;
import com.scen.sys.domain.Permission;
import com.scen.sys.service.PermissionService;
import com.scen.sys.vo.PermissionVo;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

	@Autowired
	private PermissionService permissionService;

	/**************************菜单管理开始*****************************/

	/**加载权限管理左边的权限树的json
	 * @param menuVo
	 * @return
	 */
	@RequestMapping("loadPermissionManagerLeftTreeJson")
	public DataGridView loadMenuManagerLeftTreeJson(PermissionVo permissionVo) {
		QueryWrapper<Permission> queryWrapper=new QueryWrapper<Permission>();
		queryWrapper.eq("type", constast.TYPE_MENU);
		List<Permission> list=this.permissionService.list(queryWrapper);
		List<TreeNode> treeNodes=new ArrayList<>();
		for (Permission menu : list) {
			Boolean spread=menu.getOpen()==1?true:false;
			treeNodes.add(new TreeNode(menu.getId(), menu.getPid(), menu.getTitle(), spread));
		}
		return new  DataGridView(treeNodes);
	}
	/**
	 * 查询
	 */
	@RequestMapping("loadAllPermission")
	public DataGridView loadAllPermission(PermissionVo permissionVo) {
		IPage<Permission> page=new Page<>(permissionVo.getPage(), permissionVo.getLimit());
		QueryWrapper<Permission> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("type", constast.TYPE_PERMISSION);//只能查询菜单
		queryWrapper.like(StringUtils.isNotBlank(permissionVo.getTitle()), "title", permissionVo.getTitle());
		queryWrapper.eq(permissionVo.getId()!=null, "id", permissionVo.getId()).or().eq(permissionVo.getId()!=null, "pid", permissionVo.getId());
		queryWrapper.orderByAsc("ordernum"); 
		this.permissionService.page(page, queryWrapper);
		return new DataGridView(page.getTotal(), page.getRecords());
	}
	/**添加权限
	 * @param permissionVo
	 * @return
	 */
	@RequestMapping("addPermission")
	public ResultObj addPermission(PermissionVo permissionVo) {
		try {
			permissionVo.setType(constast.TYPE_PERMISSION);//设置添加类型
			this.permissionService.save(permissionVo);
			return ResultObj.ADD_SUCCESS;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResultObj.ADD_ERROR;
		}
	}
	/**修改权限
	 * @param permissionVo
	 * @return
	 */
	@RequestMapping("updatePermission")
	public ResultObj updatePermission(PermissionVo permissionVo) {
		try {
			this.permissionService.updateById(permissionVo);
			return ResultObj.UPDATE_SUCCESS;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResultObj.UPDATE_ERROR;
		}
	}
	
	/**加载最大排序吗
	 * @return
	 */
	@RequestMapping("loadPermissionMaxOrderNum")
	public Map<String, Object> loadPermissionMaxOrderNum(){
		Map<String, Object> map=new HashMap<>();
		QueryWrapper<Permission> queryWrapper=new QueryWrapper<>();
		queryWrapper.orderByDesc("ordernum");
		List<Permission> list = permissionService.list(queryWrapper);
		if (list.size()>0) {
			map.put("value", list.get(0).getOrdernum()+1);
		}else {
			map.put("value", 1);
		}
		return map;
	}
	 
	/**删除权限
	 * @param permissionVo
	 * @return
	 */
	@RequestMapping("deletePermission")
	public ResultObj deletePermission(PermissionVo permissionVo) {
		try {
			this.permissionService.removeById(permissionVo);
			return ResultObj.DELETE_SUCCESS;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResultObj.DELETE_ERROR;
		}
	}
	
	/**************************菜单管理结束*****************************/

}
